/**
 * ApplicationContextUtil.java - 2013-4-17
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * Project Info: China UnionPay Internet Acquiring Project
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *        
 * =============================================================================
 */
package com.lijin.workshop.util;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Description: 
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class ApplicationContextUtil implements ApplicationContextAware, DisposableBean {

	private static final Logger logger = Logger.getLogger(ApplicationContextUtil.class);
	
	private static ApplicationContext ctx = null;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		if (ctx != null) {
			logger.info("ctx 已经存在，将被覆盖:" + ctx);
		}
		ctx = applicationContext;
	}

	@Override
	public void destroy() throws Exception {
		ctx = null;
	}

	/**
	 * 从Spring容器中按名称获取指定的Bean
	 * @param <T>
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		AssertUtil.validateObjNull(ctx, "Spring容器为null");
		return (T) ctx.getBean(name);
	}
}
